brute force implementation math number theory *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define endl "\n"
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;

#define vec(name) vector<ll> name;
#define all(vector) vector.begin(), vector.end()
#define sortde(vector) sort(all(vector), greater<ll>());

#define invecll(n, name)       \
    vector<ll> name;           \
    for (ll i = 0; i < n; i++) \
    {                          \
        ll lwjfh;              \
        cin >> lwjfh;          \
        name.push_back(lwjfh); \
    }

#define vecp(vector)                       \
    for (ll i = 0; i < vector.size(); i++) \
        cout << vector[i] << " ";          \
    cout << endl;

#define inll(name) \
    ll name;       \
    cin >> name;

#define ins(name) \
    string name;  \
    cin >> name;

ll sumvec(vector<ll> v);
bool isPrime(ll n);
ll binpow(ll a, ll b);

ll fin(ll k, ll n)
{

    if (n < k)
        return 0;
    if (n == k)
        return 1;

    ll ans = 0;

    for (ll i = 0; i < k; i++)
    {
        ans += fin(k, n - i - 1);
    }

    return ans;
}


// ----------------x------------------------x------------------------------x----------------------

//

// -----------------------x----------------------------------x------------------------------------

void solve()
{
    inll(n) inll(b)

    ll temp = n;
    n = b;

    map<ll,ll> mp;
    while (n % 2 == 0)
    {
        mp[2]++;
        n = n/2;
    }
 
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
           mp[i]++;
            n = n/i;
        }
    }
 
  
    if (n > 2)mp[n]++;

   ll ans =-1;
    for(auto itr : mp){

        ll mod = itr.second;
        ll num = 1;
        ll largest = itr.first;
        ll as = 0;

   while(num <=( temp/largest)){

        num*= largest;
        as+= (temp/num);
   }

   
   if(ans == -1) ans = as/mod; else 
    ans = min(ans,as/mod);
    }


   cout<<ans<<endl;
   

    
    
}

// ----------------x-----------------main---------------------------x----------------------------

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

#endif

    ll test;
    test = 1;
    // cin >> test;

    for (ll i = 1; i <= test; i++)
    {

        solve();
    }

    return 0;
}

// ----------------x---------------x----------------------------x----------------------------

//

// ----------------x-----------------------x-------------------------------------x-----------

// functions

ll sumvec(vector<ll> v)
{

    ll sum = 0;

    for (ll i = 0; i < v.size(); i++)
    {
        sum += v[i];
    }

    return sum;
}

bool isPrime(ll n)
{

    if (n == 0 || n == 1)
    {
        return false;
    }
    if (n == 2)
    {
        return true;
    }
    
    for (ll i = 3; i*i <= n; i+=2)
    {
        if(n%i ==0) return false;
    }

    return true;
    
}

ll binpow(ll a, ll b)
{
    if (b == 0)
        return 1;
    ll res = binpow(a, b / 2);
    if (b % 2 == 0)
        return (res * res);
    else
        return (res * res * a);
}


Comments

Submit
0 Comments
More Questions

274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence